package com.wyth.lottery.activity;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wyth.lottery.R;
import com.wyth.lottery.base.BaseActivity;
import com.wyth.lottery.bean.SSQHomeBean;
import com.wyth.lottery.service.Entity.Detail;
import com.wyth.lottery.service.presenter.DetailPresenter;
import com.wyth.lottery.service.view.DetailView;
import com.wyth.lottery.utils.BuildParamUtils;
import com.wyth.lottery.utils.MyApplication;

import org.greenrobot.eventbus.EventBus;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by phh on 2017/10/30.
 * <p>
 * 双色球home页
 */

public class SSQHomeActivity extends BaseActivity {

    private String detailInfo;


    Handler mHandler = new Handler();

    Runnable mRunnable = new Runnable() {
        @Override
        public void run() {

            mDetailPresenter.getDetail(detailInfo);
        }
    };

    @BindView(R.id.present_ssq)
    TextView mPresentSsq;
    @BindView(R.id.state_data)
    TextView mStateData;
    @BindView(R.id.lottery_data)
    TextView mLotteryData;
    @BindView(R.id.all_money)
    TextView mAllMoney;
    @BindView(R.id.today_tv1)
    TextView mTodayTv1;
    @BindView(R.id.today_tv2)
    TextView mTodayTv2;
    @BindView(R.id.today_tv3)
    TextView mTodayTv3;
    @BindView(R.id.today_tv4)
    TextView mTodayTv4;
    @BindView(R.id.today_tv5)
    TextView mTodayTv5;
    @BindView(R.id.today_tv6)
    TextView mTodayTv6;
    @BindView(R.id.today_tv7)
    TextView mTodayTv7;
    @BindView(R.id.yesterday_data)
    TextView mYesterdayData;
    @BindView(R.id.yesterday_tv1)
    TextView mYesterdayTv1;
    @BindView(R.id.yesterday_tv2)
    TextView mYesterdayTv2;
    @BindView(R.id.yesterday_tv3)
    TextView mYesterdayTv3;
    @BindView(R.id.yesterday_tv4)
    TextView mYesterdayTv4;
    @BindView(R.id.yesterday_tv5)
    TextView mYesterdayTv5;
    @BindView(R.id.yesterday_tv6)
    TextView mYesterdayTv6;
    @BindView(R.id.yesterday_tv7)
    TextView mYesterdayTv7;
    @BindView(R.id.one_data)
    TextView mOneData;
    @BindView(R.id.one_tv1)
    TextView mOneTv1;
    @BindView(R.id.one_tv2)
    TextView mOneTv2;
    @BindView(R.id.one_tv3)
    TextView mOneTv3;
    @BindView(R.id.one_tv4)
    TextView mOneTv4;
    @BindView(R.id.one_tv5)
    TextView mOneTv5;
    @BindView(R.id.one_tv6)
    TextView mOneTv6;
    @BindView(R.id.one_tv7)
    TextView mOneTv7;
    private DetailPresenter mDetailPresenter;


    @Override
    protected int getContentID() {

        return R.layout.activity_ssq_home;
    }

    @Override
    protected void initView() {
        String stickyEvent = EventBus.getDefault().getStickyEvent(String.class);
        if (stickyEvent != null) {
            EventBus.getDefault().removeStickyEvent(stickyEvent);
        }

    }

    @Override
    protected void initDate() {
        initData();
    }


    @Override
    protected void onPause() {
        mHandler.removeCallbacks(mRunnable);
        super.onPause();
    }

    @OnClick({R.id.iv_bz_button, R.id.iv_dt_button, R.id.tv_return})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_bz_button:
                startActivity(new Intent(MyApplication.getContext(), SSQSelectActivity.class));
                finish();
                break;
            case R.id.iv_dt_button:
                EventBus.getDefault().postSticky("DT");
                startActivity(new Intent(MyApplication.getContext(), SSQSelectActivity.class));
                finish();
                break;
            case R.id.tv_return:
                finish();
                break;
        }
    }

    private void initData() {
        mDetailPresenter = new DetailPresenter(this);
        Gson gson = new Gson();
        Map<String, String> param = new HashMap<String, String>();
        param.put("sid", "yhl");
        param.put("cmd", "3301");
        param.put("platform", "andorid");
        param.put("sysVer", "android4.2");
        param.put("softVer", "v1.0");
        param.put("width", "800");
        param.put("height", "1024");
        param.put("machId", "aaaaa");
        param.put("func", "history");
        param.put("pageSize", "3");


        String msgs = "{\"lotteryId\":\"001\"}";
        param.put("msg", msgs);
        String currMd5 = BuildParamUtils.buildMd5(param, "yhlkey");
        param.put("md5", currMd5);
        System.out.println(msgs+"----------------");
        detailInfo = gson.toJson(param);

        mHandler.post(mRunnable);

        mDetailPresenter.onCreate();
        mDetailPresenter.attachView(mBookView);

    }

    public void setPresent(SSQHomeBean.IssueListBean present) {
        mPresentSsq.setText(present.getName());
        mLotteryData.setText(present.getEndTime());
        mStateData.setText(present.getGlobalSaleTotal());
        mAllMoney.setText(present.getPrizePool());
        String bonusNumber = present.getBonusNumber();
        String[] strings = bonusNumber.split("\\*");
        setText(mTodayTv1, strings[0]);
        setText(mTodayTv2, strings[1]);
        setText(mTodayTv3, strings[2]);
        setText(mTodayTv4, strings[3]);
        setText(mTodayTv5, strings[4]);
        setText(mTodayTv6, strings[5]);
        setText(mTodayTv7, strings[6]);
    }

    public void setText(TextView text, String s) {
        text.setText(s);
    }

    public void setYesterday(SSQHomeBean.IssueListBean yesterday) {
        mYesterdayData.setText(yesterday.getName());
        String bonusNumber = yesterday.getBonusNumber();
        String[] split = bonusNumber.split("\\*");
        setText(mYesterdayTv1, split[0]);
        setText(mYesterdayTv2, split[1]);
        setText(mYesterdayTv3, split[2]);
        setText(mYesterdayTv4, split[3]);
        setText(mYesterdayTv5, split[4]);
        setText(mYesterdayTv6, split[5]);
        setText(mYesterdayTv7, split[6]);
    }

    public void setOne(SSQHomeBean.IssueListBean yesterday) {
        setText(mOneData, yesterday.getName());
        String bonusNumber = yesterday.getBonusNumber();
        String[] split = bonusNumber.split("\\*");
        setText(mOneTv1, split[0]);
        setText(mOneTv2, split[1]);
        setText(mOneTv3, split[2]);
        setText(mOneTv4, split[3]);
        setText(mOneTv5, split[4]);
        setText(mOneTv6, split[5]);
        setText(mOneTv7, split[6]);
    }

    private DetailView mBookView = new DetailView() {

        @Override
        public void onSuccess(Detail detail) {
            Gson gson = new Gson();
            SSQHomeBean ssqHomeBean = gson.fromJson(detail.getResult(), SSQHomeBean.class);
            List<SSQHomeBean.IssueListBean> issueList = ssqHomeBean.getIssueList();
            setPresent(issueList.get(0));
            setYesterday(issueList.get(1));
            setOne(issueList.get(2));
            System.out.println("接受到数据---------------------");
        }

        @Override
        public void onError(String result) {
            Toast.makeText(SSQHomeActivity.this, result, Toast.LENGTH_SHORT).show();
        }
    };


}
